package com.likai.blog.service.impl;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.likai.blog.pojo.Blog;
import com.likai.blog.pojo.BlogType;
import com.likai.blog.pojo.Blogger;
import com.likai.blog.pojo.Link;
import com.likai.blog.service.IBlogService;
import com.likai.blog.service.IBlogTypeService;
import com.likai.blog.service.IBloggerService;
import com.likai.blog.service.ILinkService;

@Component
public class InitComponent implements ServletContextListener,ApplicationContextAware {

	private static ApplicationContext applicationContext ;
	
	public void contextDestroyed(ServletContextEvent arg0) {
		
	}

	public void contextInitialized(ServletContextEvent arg0) {
		//取得application
		ServletContext application = arg0.getServletContext() ;
		
		//取得bloggerService
		IBloggerService bloggerService = (IBloggerService) applicationContext.getBean("bloggerService") ;
		//取得linkService 
		ILinkService linkService = (ILinkService) applicationContext.getBean("linkService") ;
		//取得blogTypeService
		IBlogTypeService blogTypeService = (IBlogTypeService) applicationContext.getBean("blogTypeService") ;
		//取得bloggerService
		IBlogService blogService = (IBlogService) applicationContext.getBean("blogService") ;
				
		//取得blogger对象
		Blogger blogger = bloggerService.find() ;
		//取得link列表
		List<Link> linkList = linkService.findList() ;
		//取得博客类型与对应数量列表
		List<BlogType> typeList = blogTypeService.findListAndCount() ;
		//取得按日期分组的博客数量List
		List<Blog> blogList = blogService.findListGroupDate() ;
		
		blogger.setPassword(null);
		
		//将blogger的信息放入application中
		application.setAttribute("blogger", blogger);
		//将link列表的信息放入application
		application.setAttribute("linkList", linkList);
		//将type列表的信息放入application
		application.setAttribute("typeList", typeList);
		//将blog列表的信息放入application
		application.setAttribute("blogTJList", blogList);
		
	}

	/**
	 * spring接口提供的方法
	 */
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

		this.applicationContext = applicationContext ;
	}

}
